package com.game.league.articles;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.game.base.app.base.BaseFragment;
import com.game.base.observe.MsgMgr;
import com.game.base.observe.MsgType;
import com.game.base.utils.DecimalUtil;
import com.game.base.utils.log.Logger;
import com.game.base.utils.toast.ToastUtil;
import com.game.league.R;
import com.game.league.articles.panel.ArticlesPanel;
import com.game.league.articles.table.Banner;
import com.game.league.databinding.FragmentArticlesBinding;
import com.game.league.entrance.user.UserMgr;
import com.game.league.entrance.user.table.User;
import com.game.league.service.InterfaceMgr;
import com.game.league.util.Constants;
import com.game.league.util.SkipUtil;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.widget.popup.QMUIPopup;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;

/**
 * 物品页
 * Created by Su on 2019/5/20.
 */
public class ArticlesFragment extends BaseFragment<FragmentArticlesBinding> implements View.OnClickListener {
    private User user;
    private ArticlesPanel propsPanel, skinPanel, heroPanel;
    private String qqGameId, wxGameId;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_articles;
    }

    @Override
    protected void initView() {
        binding.setTitle("物品");
        binding.setClick(this);
        refreshView();

        qqGameId = InterfaceMgr.getConfig().getQqGameID();
        wxGameId = InterfaceMgr.getConfig().getWxGameID();
        if (!TextUtils.isEmpty(qqGameId)) {
            binding.tvGameId.setText(qqGameId + ";" + wxGameId);
        }

        propsPanel = new ArticlesPanel(mActivity, Constants.TYPE_PROPS);
        skinPanel = new ArticlesPanel(mActivity, Constants.TYPE_SKIN);
        heroPanel = new ArticlesPanel(mActivity, Constants.TYPE_HERO);
        addPanel(skinPanel);
    }

    public void refreshView() {
        user = UserMgr.getCurrentUser();
        binding.tvCoinRemain.setText(DecimalUtil.getFormatFloat(user.getCoinRemain()));
        binding.tvCoinToday.setText("今日收入：" + DecimalUtil.getFormatFloat(user.getCoinToday()));
        binding.tvCoinHistory.setText("历史收入：" + DecimalUtil.getFormatFloat(user.getCoinHistory()));
    }

    @Override
    protected void initData() {
        initBanner();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_right_iv:
                SkipUtil.showAboutActivity(getActivity());
                break;
            case R.id.btn_task:
                MsgMgr.getInstance().sendMsg(MsgType.MT_Switch_Task);
                break;

            case R.id.btn_props:
                binding.btnProps.setChecked(true);
                binding.btnSkin.setChecked(false);
                binding.btnHero.setChecked(false);
                addPanel(propsPanel);
                break;
            case R.id.btn_skin:
                binding.btnProps.setChecked(false);
                binding.btnSkin.setChecked(true);
                binding.btnHero.setChecked(false);
                addPanel(skinPanel);
                break;
            case R.id.btn_hero:
                binding.btnProps.setChecked(false);
                binding.btnSkin.setChecked(false);
                binding.btnHero.setChecked(true);
                addPanel(heroPanel);
                break;

            case R.id.ll_kefu:
                showNormalPopup();
                break;
        }
    }

    private void addPanel(ArticlesPanel panel) {
        binding.container.removeAllViews();
        binding.container.addView(panel.getContentView());
    }

    private void initBanner() {
        InterfaceMgr.getInstance().getBannerList(Constants.BANNER_PIC, new FindListener<Banner>() {
            @Override
            public void done(List<Banner> list, BmobException e) {
                if (e != null || list == null || list.isEmpty()) {
                    binding.banner.setVisibility(View.GONE);
                    return;
                }
                List<String> images = new ArrayList<>();
                for (Banner banner : list) {
                    images.add(banner.getImgUrl());
                }
                initBannerView(images);
                binding.banner.setVisibility(View.VISIBLE);
            }
        });
    }

    private void initBannerView(List<String> images) {
        binding.banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR)
                .setImages(images)
                .setBannerAnimation(Transformer.ZoomOutSlide)
                .setDelayTime(4000)
                .setIndicatorGravity(BannerConfig.RIGHT)
                .setImageLoader(new ImageLoader() {
                    @Override
                    public void displayImage(Context context, Object path, ImageView imageView) {
                        com.game.base.glideV4.ImageLoader.loadRoundCenterCrop(getContext(), (String) path, imageView);
                    }
                })
                .setOnBannerListener(position -> Logger.d(position + "")).start();
    }

    // pop浮窗
    private QMUIPopup mNormalPopup;

    private void showNormalPopup() {
        if (TextUtils.isEmpty(qqGameId)) {
            ToastUtil.showLong("配置信息获取失败，请退出重试或联系客服");
            return;
        }

        if (mNormalPopup == null) {
            mNormalPopup = new QMUIPopup(mActivity, QMUIPopup.DIRECTION_NONE);
            TextView textView = new TextView(getContext());
            textView.setLayoutParams(mNormalPopup.generateLayoutParam(
                    QMUIDisplayHelper.dp2px(mActivity, 180),
                    WRAP_CONTENT
            ));
            textView.setLineSpacing(QMUIDisplayHelper.dp2px(getContext(), 4), 1.0f);
            int padding = QMUIDisplayHelper.dp2px(getContext(), 20);
            textView.setPadding(padding, padding, padding, padding);
            textView.setText("温馨提示：\n物品以邮箱赠送形式发放，兑换后请及时添加客服游戏ID为好友哦！\n\n" + qqGameId + "\n" + wxGameId);
            textView.setTextColor(ContextCompat.getColor(getContext(), R.color.white));
            textView.setBackgroundColor(mActivity.getResources().getColor(R.color.black_d5));
            mNormalPopup.setContentView(textView);
            mNormalPopup.setOnDismissListener(null);
        }

        mNormalPopup.setAnimStyle(QMUIPopup.ANIM_GROW_FROM_CENTER);
        mNormalPopup.setPreferredDirection(QMUIPopup.DIRECTION_BOTTOM);
        mNormalPopup.show(binding.arrowDown);
    }
}
